package com.sqlist.shareserver.controller;

import com.sqlist.shareserver.domain.User;
import com.sqlist.shareserver.dto.LoginResultDTO;
import com.sqlist.shareserver.dto.MessageDTO;
import com.sqlist.shareserver.dto.RegisterDTO;
import com.sqlist.shareserver.service.UserService;
import com.sqlist.shareserver.util.Const;
import com.sqlist.shareserver.util.TokenFactory;
import com.sqlist.shareserver.util.TokenPool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public MessageDTO register(User user) {
        RegisterDTO registerDTO = new RegisterDTO();
        MessageDTO<RegisterDTO> registerDTOMessageDTO = new MessageDTO<>();
        registerDTOMessageDTO.setCode(Const.REQUEST_SUCCESS);
        registerDTOMessageDTO.setData(registerDTO);

        User registerUser = userService.register(user);
        if (registerUser != null) {
            if (registerUser.getUserId() != null) {
                registerDTO.setStatus(RegisterDTO.REGISTER_SUCCESS);
                registerDTOMessageDTO.setMsg("注册成功");
            } else {
                registerDTO.setStatus(RegisterDTO.REGISTER_FAIL);
                registerDTOMessageDTO.setMsg("用户名已存在，注册失败");
            }
        } else {
            registerDTO.setStatus(RegisterDTO.REGISTER_FAIL);
            registerDTOMessageDTO.setMsg("注册失败");
        }
        return registerDTOMessageDTO;
    }


    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public MessageDTO login(User user) {
        log.info("要进行登录的用户 " + user.getUsername());
        log.debug("username: " + user.getUsername() + ", password: " + user.getPassword());
        User resultUser = userService.login(user);
        LoginResultDTO loginResultDTO = new LoginResultDTO();

        MessageDTO<LoginResultDTO> loginResultDTOMessageDTO = new MessageDTO<>();
        loginResultDTOMessageDTO.setCode(Const.REQUEST_SUCCESS);
        loginResultDTOMessageDTO.setType("loginResultDTO");
        loginResultDTOMessageDTO.setData(loginResultDTO);

        if (resultUser != null) {
            log.info("登录成功");
            loginResultDTO.setStatus(LoginResultDTO.LOGIN_SUCCESS);

            loginResultDTOMessageDTO.setMsg("登录成功");
            loginResultDTOMessageDTO.setUsername(resultUser.getUsername());
            loginResultDTOMessageDTO.setToken(TokenFactory.createToken());
        } else {
            log.info("登录失败");
            loginResultDTO.setStatus(LoginResultDTO.LOGIN_FAIL);

            loginResultDTOMessageDTO.setMsg("登录失败，用户名或密码错误");
        }
        return loginResultDTOMessageDTO;
    }

    @ResponseBody
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    public MessageDTO logout(String username, String token) {
        TokenPool.removeToken(token);

        MessageDTO<LoginResultDTO> messageDTO = new MessageDTO();
        messageDTO.setCode(Const.REQUEST_SUCCESS);
        messageDTO.setUsername(username);
        messageDTO.setMsg("退出成功");

        return messageDTO;
    }
}
